package com.ljkj.blockpay.presenter;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.JsonResponseListener;
import com.kongzue.baseokhttp.util.JsonBean;
import com.kongzue.baseokhttp.util.JsonList;
import com.kongzue.baseokhttp.util.JsonMap;
import com.kongzue.baseokhttp.util.Parameter;
import com.ljkj.blockpay.basedata.BasePresenterImpl;
import com.ljkj.blockpay.basedata.BaseView;
import com.ljkj.blockpay.model.RechargeModel;
import com.ljkj.blockpay.network.ApiToCConfig;
import com.ljkj.blockpay.presenter.inter.ExchangeInter;
import com.ljkj.blockpay.widget.LoadingDialog;
import com.luck.picture.lib.tools.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yp on 2022/2/28
 */
public class ExchangePresenter extends BasePresenterImpl<ExchangeInter.ExchangeInterView> implements ExchangeInter.ExchangeInterPresenter {

    public String preOrderId;
    private LoadingDialog loadingDialog;
//    public void showLoading() {
//        if (loadingDialog == null) {
//            loadingDialog = new LoadingDialog.Builder(getContext()).create();
//        }
//        loadingDialog.show();
//    }
//
//    public void dismisLoading() {
//        if (loadingDialog != null) {
//            loadingDialog.dismiss();
//        }
//    }
    public ExchangePresenter(@NonNull Context context, @NonNull ExchangeInter.ExchangeInterView view) {
        super(context, view);
    }


    //查询币种余额
    @Override
    public void SearchCurrencyBalance(String currencyType, Integer orderType) {
//        showLoading();
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("currencyType",currencyType);
        jsonMap.put("orderType",orderType);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_SEARCH_CURRENCY, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
//                    dismisLoading();
                    JsonMap jsondata= main.getJsonMap("data");
                    getView().SearchCurrencySuccess(jsondata);

                } else {
//                    dismisLoading();
                }
            }
        });
    }

    //业务场景支持币种and网络精度
    @Override
    public void getNetAccuracy() {
//        showLoading();
        HttpRequest.GET(getContext(), ApiToCConfig.URL_TRADE_NETWORK_ACCURACY, new Parameter().add("orderType", "5"), new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    JsonList jsonList = main.getList("data");
                    List<RechargeModel> list = new ArrayList<>();
                    for (int i = 0; i < jsonList.size(); i++) {
                        list.add(JsonBean.getBean(jsonList.getJsonMap(i), RechargeModel.class));
                    }
                    getView().showNetAccuracy(list);
                    Log.d("zdl","==onSafeSuccess==="+list.size());
                }else{
                }
            }
        });
    }

    //订单预创建
    @Override
    public void preConfirm(Number originAmount, Integer orderType,String originCurrency,String targetCurrency) {
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("bizToken","");
        jsonMap.put("orderType",orderType);
        jsonMap.put("networkType","");
        jsonMap.put("originAddress","");
        jsonMap.put("originAmount",originAmount);
        jsonMap.put("originCurrency",originCurrency);
        jsonMap.put("remark","");
        jsonMap.put("targetAddress","");
        jsonMap.put("targetCurrency",targetCurrency);
        jsonMap.put("targetUser","");
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_ORDER_PRECONFIRM, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0 ){
                    JsonMap jsonMapData = main.getJsonMap("data");
                    getView().preConfirmSuccess(jsonMapData);
                } else if (main.getInt("code") == 400){


                }
            }
        });
    }

    //创建订单
    @Override
    public void createOrder(Integer orderType,String preOrderId, String tradePwd) {
//        showLoading();
        if (preOrderId == null){
            ToastUtils.s(getContext(),"未获取到订单id");
            return;
        }
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("orderType",orderType);
        jsonMap.put("preOrderId",preOrderId);
        jsonMap.put("tradePwd",tradePwd);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_ORDER_CREATE, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
//                    dismisLoading();
                    JsonMap jsonMapData =   main.getJsonMap("data");
                    getView().getOrderDetails2(jsonMapData);
                } else {
//                    dismisLoading();
                }
            }
        });
    }
}
